#!/bin/sh

DIR=`dirname $0`
OPTION=$1

if test "$OPTION" != "-test" -a "`echo $UID`" != "0"; then
  echo "You must be root to use this program!"
  exit 0
fi

function readlink()
{
  ls -l $1|awk '{print $NF}'
}

pushd $DIR > /dev/null; BINDIR=`pwd`; popd > /dev/null

# only remove the symbolic links if they do not point to other OpenGL
# libraries, which do exist; switch to mesasoft if the package is 
# installed

if test "$OPTION" == "-removelinks"; then
for libdir in lib; do
( cd $DIR/../../$libdir
  if test `readlink libGL.so.1`; then
    if test `readlink libGL.so.1` == "GL/libGL.so.1.2.xf86_glx"; then 
      rm -f libGL.so.1 libMesaGL.so.3 libGL.so
      test -x $BINDIR/switch2mesasoft
      if [ $? -eq 0 ]; then
        $BINDIR/switch2mesasoft
        test -x $DIR/../../bin/chrc.config && \
          $DIR/../../bin/chrc.config SCRIPT_3D switch2mesasoft
      else
        test -x $DIR/../../bin/chrc.config && \
          $DIR/../../bin/chrc.config SCRIPT_3D no
      fi                                      
    elif ! test -f `readlink libGL.so.1`; then
      rm -f libGL.so.1 libMesaGL.so.3 libGL.so
      test -x $BINDIR/switch2mesasoft
      if [ $? -eq 0 ]; then
        $BINDIR/switch2mesasoft
        test -x $DIR/../../bin/chrc.config && \
          $DIR/../../bin/chrc.config SCRIPT_3D switch2mesasoft
      else
        test -x $DIR/../../bin/chrc.config && \
          $DIR/../../bin/chrc.config SCRIPT_3D no
      fi                                      
    fi
  fi
)
done
test -d $DIR/../lib/modules/extensions && \
( cd $DIR/../lib/modules/extensions
  if test `readlink libGLcore.a`; then
    if test `readlink libGLcore.a` == "libGLcore.a.xf86_glx"; then
      rm -f libGLcore.a
    else
      test -f `readlink libGLcore.a` || rm -f libGLcore.a
    fi
  fi
  if test `readlink libglx.a`; then
    if test `readlink libglx.a` == "libglx.a.xf86_glx"; then
      rm -f libglx.a
    else
      test -f `readlink libglx.a` || rm -f libglx.a
    fi
  fi
)
exit 0

elif test "$OPTION" == "-test"; then
for libdir in lib; do
pushd $DIR/../../$libdir > /dev/null
  test -L libGL.so.1 || exit 1
  test `readlink libGL.so.1` || exit 1
  test `readlink libGL.so.1` == "GL/libGL.so.1.2.xf86_glx" || exit 1
popd > /dev/null
done
test -d $DIR/../lib/modules/extensions && \
( pushd $DIR/../lib/modules/extensions > /dev/null
  test -L libGLcore.a || exit 1
  test `readlink libGLcore.a` || exit 1
  test `readlink libGLcore.a` == "libGLcore.a.xf86_glx" || exit 1
  test -L libglx.a || exit 1
  test `readlink libglx.a` || exit 1
  test `readlink libglx.a` == "libglx.a.xf86_glx" || exit 1
  test -e libglx.so && exit 1
  popd > /dev/null
)
exit 0

else
for libdir in lib; do
( cd $DIR/../../$libdir
  GL=`readlink libGL.so.1 2> /dev/null`
  if test ! "x$GL" == "xGL/libGL.so.1.2.xf86_glx"; then
    ln -sf GL/libGL.so.1.2.xf86_glx libGL.so.1
    ln -sf libGL.so.1 libGL.so
    ln -sf libGL.so.1 libMesaGL.so.3 
  fi
)
done
test -d $DIR/../lib/modules/extensions && \
( cd $DIR/../lib/modules/extensions
  ln -sf libGLcore.a.xf86_glx libGLcore.a
  ln -sf libglx.a.xf86_glx libglx.a
  rm -f libglx.so
)
test -x $DIR/../../bin/chrc.config && \
  $DIR/../../bin/chrc.config SCRIPT_3D switch2xf86_glx
fi
exit 0
